package com.ruoyi.question.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.DtQuestion;
import com.ruoyi.question.service.IDtQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 题库Controller
 *
 * @author llc
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/question/questions")
public class DtQuestionController extends BaseController {
    @Autowired
    private IDtQuestionService dtQuestionService;

// 随机题库
@PreAuthorize("@ss.hasPermi('question:questions:random')")
@GetMapping("/random")
public AjaxResult random(@RequestParam Integer count,DtQuestion question) {
    List<DtQuestion> questions = dtQuestionService.selectDtQuestionList(question);

    // 检查是否有足够的题目
    if (questions.isEmpty()) {
        return AjaxResult.error("没有找到符合条件的题目");
    }

    // 确保 count 不超过题目数量
    int actualCount = Math.min(count, questions.size());

    Collections.shuffle(questions);
    List<DtQuestion> result = questions.subList(0, actualCount);

    return AjaxResult.success(result);
}


    /**
     * 查询题库列表
     */
    @PreAuthorize("@ss.hasPermi('question:questions:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtQuestion dtQuestion) {
        startPage();
        List<DtQuestion> list = dtQuestionService.selectDtQuestionList(dtQuestion);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    @PreAuthorize("@ss.hasPermi('question:questions:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtQuestion dtQuestion) {
        List<DtQuestion> list = dtQuestionService.selectDtQuestionList(dtQuestion);
        ExcelUtil<DtQuestion> util = new ExcelUtil<DtQuestion>(DtQuestion.class);
        util.exportExcel(response, list, "题库数据");
    }

    /**
     * 获取题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:questions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dtQuestionService.selectDtQuestionById(id));
    }

    /**
     * 新增题库
     */
    @PreAuthorize("@ss.hasPermi('question:questions:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtQuestion dtQuestion) {
        return toAjax(dtQuestionService.insertDtQuestion(dtQuestion));
    }

    /**
     * 修改题库
     */
    @PreAuthorize("@ss.hasPermi('question:questions:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtQuestion dtQuestion) {
        return toAjax(dtQuestionService.updateDtQuestion(dtQuestion));
    }

    /**
     * 删除题库
     */
    @PreAuthorize("@ss.hasPermi('question:questions:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dtQuestionService.deleteDtQuestionByIds(ids));
    }

}
